/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.ruoyi.mall.dubbo;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.mall.api.RemoteUserAddressService;
import com.ruoyi.mall.api.constant.CommonConstants;
import com.ruoyi.mall.api.entity.UserAddress;
import com.ruoyi.mall.mapper.UserAddressMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * 用户收货地址
 *
 * @author JL
 * @date 2019-09-11 14:28:59
 */
@Service
@DubboService
public class RemoteUserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements RemoteUserAddressService {

    @Override
    public boolean save(UserAddress entity) {
        setIsDefault(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(UserAddress entity) {
        setIsDefault(entity);
        return super.updateById(entity);
    }

    /**
     * 修改默认地址
     *
     * @param entity
     */
    void setIsDefault(UserAddress entity) {
        if (CommonConstants.YES.equals(entity.getIsDefault())) {
            UserAddress userAddress = new UserAddress();
            userAddress.setIsDefault(CommonConstants.NO);
            super.update(userAddress, Wrappers.<UserAddress>lambdaQuery()
                .eq(UserAddress::getUserId, entity.getUserId())
                .eq(UserAddress::getIsDefault, CommonConstants.YES));
        }
    }

    @Override
    public UserAddress getById(String id) {
        return super.getById(id);
    }

    @Override
    public IPage page(Page page, UserAddress userAddress) {
        return super.page(page, Wrappers.query(userAddress));
    }

    @Override
    public boolean saveOrUpdate(UserAddress entity) {
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean removeById(String id) {
        return super.removeById(id);
    }
}
